package system

import (
	"github.com/gin-gonic/gin"
	"go-admin/app/controllers"
	"go-admin/app/global/app"
	"go-admin/app/models/params"
)

type ConfigController struct {
	controllers.BaseResult
}

// NewConfigController 创建配置控制器
func NewConfigController() *ConfigController {
	return &ConfigController{
		BaseResult: controllers.BaseResult{},
	}
}

// GetConfig 获取配置信息
// @Summary 获取配置信息
// @Description 获取系统配置信息
// @Tags 配置管理
// @Accept json
// @Produce json
// @Success 200 {object} map[string]interface{} "成功返回配置信息"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /config/get [get]
func (con ConfigController) GetConfig(ctx *gin.Context) {
	// 提取Server、HttpServer和Captcha配置
	result := make(map[string]interface{})

	// 获取Server配置
	systemConfig := make(map[string]interface{})

	systemConfig["systemLogo"] = app.ConfigYml.GetString("system.systemLogo")
	systemConfig["systemIcon"] = app.ConfigYml.GetString("system.systemIcon")
	systemConfig["systemName"] = app.ConfigYml.GetString("system.systemName")
	systemConfig["systemCopyright"] = app.ConfigYml.GetString("system.systemCopyright")
	systemConfig["systemRecordNo"] = app.ConfigYml.GetString("system.systemRecordNo")

	// 获取DemoAccount配置
	systemConfig["defaultUsername"] = app.ConfigYml.GetString("server.demoAccount.defaultUsername")
	systemConfig["defaultPassword"] = app.ConfigYml.GetString("server.demoAccount.defaultPassword")
	result["system"] = systemConfig

	// 获取Safe配置
	safeConfig := make(map[string]interface{})
	safeConfig["loginLockThreshold"] = app.ConfigYml.GetInt("safe.loginLockThreshold")
	safeConfig["loginLockExpire"] = app.ConfigYml.GetInt("safe.loginLockExpire")
	safeConfig["loginLockDuration"] = app.ConfigYml.GetInt("safe.loginLockDuration")
	safeConfig["minPasswordLength"] = app.ConfigYml.GetInt("safe.minPasswordLength")
	safeConfig["requireSpecialChar"] = app.ConfigYml.GetBool("safe.requireSpecialChar")
	result["safe"] = safeConfig

	// 获取Captcha配置
	captchaConfig := make(map[string]interface{})
	captchaConfig["enable"] = app.ConfigYml.GetBool("captcha.enable")
	captchaConfig["length"] = app.ConfigYml.GetInt("captcha.length")
	result["captcha"] = captchaConfig

	// 返回成功响应
	con.Success(ctx, result)
}

// UpdateConfig 更新配置信息
// @Summary 更新配置信息
// @Description 更新系统配置信息
// @Tags 配置管理
// @Accept json
// @Produce json
// @Param config body map[string]interface{} true "配置信息"
// @Success 200 {object} map[string]interface{} "成功更新配置信息"
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /config/update [put]
// @Security ApiKeyAuth
func (con ConfigController) UpdateConfig(ctx *gin.Context) {
	var req params.ConfigRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		con.BaseResult.FailAndAbort(ctx, "参数绑定失败", err)
	}

	// 更新System配置
	app.ConfigYml.Set("system.systemLogo", req.System.SystemLogo)
	app.ConfigYml.Set("system.systemIcon", req.System.SystemIcon)
	app.ConfigYml.Set("system.systemName", req.System.SystemName)
	app.ConfigYml.Set("system.systemCopyright", req.System.SystemCopyright)
	app.ConfigYml.Set("system.systemRecordNo", req.System.SystemRecordNo)

	// 更新DemoAccount配置
	app.ConfigYml.Set("server.demoAccount.defaultUsername", req.System.DefaultUsername)
	app.ConfigYml.Set("server.demoAccount.defaultPassword", req.System.DefaultPassword)

	// 更新Safe配置
	app.ConfigYml.Set("safe.loginLockThreshold", req.Safe.LoginLockThreshold)
	app.ConfigYml.Set("safe.loginLockExpire", req.Safe.LoginLockExpire)
	app.ConfigYml.Set("safe.loginLockDuration", req.Safe.LoginLockDuration)
	app.ConfigYml.Set("safe.minPasswordLength", req.Safe.MinPasswordLength)
	app.ConfigYml.Set("safe.requireSpecialChar", req.Safe.RequireSpecialChar)

	// 更新Captcha配置
	app.ConfigYml.Set("captcha.enable", req.Captcha.Enable)
	app.ConfigYml.Set("captcha.length", req.Captcha.Length)

	// 保存配置到文件
	if err := app.ConfigYml.SaveConfig(); err != nil {
		con.FailAndAbort(ctx, "保存配置文件失败", err)
	}

	// 返回成功响应
	con.SuccessWithMessage(ctx, "配置更新成功")
}
